{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['_krb_kerberos', '_krb_anonymous']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "['CONTRIBUTING.txt', 'README.md']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "['Dat1.csv', 'Dat2.csv']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = \"\"\"\n",
    "_krb_kerberos:*:233:-2:Open Directory Kerberos:/var/empty:/usr/bin/false\n",
    "_krb_anonymous:*:234:-2:Open Directory Kerberos Anonymous:/var/empty:/usr/bin/false\n",
    "\"\"\"\n",
    "\n",
    "strlist = filter(lambda line: len(line.strip()) > 0, s.splitlines())\n",
    "list(map(lambda l: l.split(\":\")[0], strlist))\n",
    "\n",
    "\n",
    "s2 = \"\"\"-rw-r--r--   1 cookeem  staff   1.1K  9  1 11:47 CODEOWNERS\n",
    "-rw-r--r--   1 cookeem  staff   1.3K  9  1 11:47 CONTRIBUTING.txt\n",
    "-rw-r--r--   1 cookeem  staff   1.9K  9  1 11:47 ISSUE_TEMPLATE.py\n",
    "-rw-r--r--   1 cookeem  staff    11K  9  1 11:47 LICENSE\n",
    "-rw-r--r--   1 cookeem  staff   3.5K  9  1 11:47 README.md\n",
    "-rw-r--r--   1 cookeem  staff     0B  9  1 11:47 WORKSPACE\"\"\"\n",
    "strlist2 = filter(lambda l: l.endswith((\".txt\", \".md\")), s2.splitlines())\n",
    "list(map(lambda l: l.split(\" \")[-1], strlist2))\n",
    "\n",
    "any([False, False])\n",
    "any([True, False])\n",
    "\n",
    "# 使用文件系统通配符匹配字符串\n",
    "\n",
    "import fnmatch\n",
    "fnmatch.fnmatch('foo.txt', '*.txt')\n",
    "fnmatch.fnmatch('foo.txt', '?oo.txt')\n",
    "fnmatch.fnmatch('Dat45.csv', 'Dat[0-9]*')\n",
    "names = ['Dat1.csv', 'Dat2.csv', 'config.ini', 'foo.py']\n",
    "[name for name in names if fnmatch.fnmatch(name, 'Dat*.csv')]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "28"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "yes\n",
      "no\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[('11', '27', '2012'), ('3', '13', '2013')]"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('11', '27', '2012')\n",
      "('3', '13', '2013')\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Today is 2012-11-27. PyCon starts 2013-3-13.'"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'Today is 2012-11-27. PyCon starts 2013-3-13.'"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "('Today is 2012-11-27. PyCon starts 2013-3-13.', 2)"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "['PYTHON', 'python', 'Python']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'UPPER snake, lower snake, Mixed snake'"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串查找\n",
    "\n",
    "text = 'yeah, but no, but yeah, but no, but yeah'\n",
    "\n",
    "text.find(\"no\", 20)\n",
    "\n",
    "text1 = '11/27/2012'\n",
    "text2 = 'Nov 27, 2012'\n",
    "\n",
    "# 如果你想使用同一个模式去做多次匹配，你应该先将模式字符串预编译为模式对象。\n",
    "import re\n",
    "# 在定义正则式的时候，通常会利用括号去捕获分组。\n",
    "datepat = re.compile(r'(\\d+)/(\\d+)/(\\d+)')\n",
    "if datepat.match(text1):\n",
    "    print('yes')\n",
    "else:\n",
    "    print('no')\n",
    "\n",
    "if datepat.match(text2):\n",
    "    print('yes')\n",
    "else:\n",
    "    print('no')\n",
    "\n",
    "text = 'Today is 11/27/2012. PyCon starts 3/13/2013.'\n",
    "datepat.findall(text)\n",
    "\n",
    "for m in datepat.finditer(text):\n",
    "    print(m.groups())\n",
    "    \n",
    "# 模式替换\n",
    "re.sub(r'(\\d+)/(\\d+)/(\\d+)', r'\\3-\\1-\\2', text)\n",
    "\n",
    "datepat.sub(r'\\3-\\1-\\2', text)\n",
    "\n",
    "newtext, n = datepat.subn(r'\\3-\\1-\\2', text)\n",
    "\n",
    "(newtext, n)\n",
    "\n",
    "\n",
    "# 字符串忽略大小写的搜索替换\n",
    "text = 'UPPER PYTHON, lower python, Mixed Python'\n",
    "re.findall('python', text, flags=re.IGNORECASE)\n",
    "re.sub('python', 'snake', text, flags=re.IGNORECASE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['no.\" Phone says \"yes.']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "['no.', 'yes.']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[' this is a comment ']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "[' this is a\\nmultiline comment ']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最短模式匹配\n",
    "\n",
    "text = 'Computer says \"no.\" Phone says \"yes.\"'\n",
    "# 这种是贪婪匹配\n",
    "str_pat = re.compile(r'\\\"(.*)\\\"')\n",
    "str_pat.findall(text)\n",
    "# 这种是最短匹配\n",
    "str_pat = re.compile(r'\\\"(.*?)\\\"')\n",
    "str_pat.findall(text)\n",
    "\n",
    "# 多行模式匹配\n",
    "text1 = '/* this is a comment */'\n",
    "text2 = '''/* this is a\n",
    "multiline comment */\n",
    "'''\n",
    "# 这种无法匹配多行\n",
    "comment = re.compile(r'/\\*(.*?)\\*/')\n",
    "comment.findall(text1)\n",
    "comment.findall(text2)\n",
    "\n",
    "# 这种进行多行比配\n",
    "comment = re.compile(r'/\\*((?:.|\\n)*?)\\*/')\n",
    "comment.findall(text2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"'My name is \\\\u5f20\\\\u4e09'\""
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# unicode支持\n",
    "\n",
    "text = \"My name is 张三\"\n",
    "ascii(text)\n",
    "len(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'This is a string \\n'"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'Hello world'"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'HelloWorld'"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "' Hello World '"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "['line1', 'line2', 'line3']"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除字符串中不需要的字符\n",
    "\n",
    "# 头尾删除特定字符\n",
    "text = \"  ---This is a string \\n ===\"\n",
    "text.strip(\" -=\")\n",
    "\" Hello world \\n\".strip()\n",
    "\n",
    "# 替换字符\n",
    "text = \"  Hello World  \"\n",
    "# 字符替换\n",
    "text.replace(\" \", \"\")\n",
    "# 模式替换\n",
    "re.sub('\\s+', ' ', text)\n",
    "\n",
    "lines = \"\"\" \n",
    " line1 \n",
    " line2 \n",
    "line3  \"\"\"\n",
    "[l.strip() for l in lines.splitlines() if len(l.strip()) > 0]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "' hello world {tab} Python {line}:)'"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用翻译表替换字符\n",
    "transtab = {\n",
    "    ord('\\t'): '{tab}',\n",
    "    ord('\\n'): '{line}',\n",
    "    ord('H'): 'h',\n",
    "}\n",
    "text = ' Hello world \\t Python \\n:)'\n",
    "text.translate(transtab)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'=========Hello World'"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'####Hello World#####'"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'         Hello World'"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'Hello World         '"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'    Hello World     '"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'=========Hello World'"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'****Hello World*****'"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串对齐\n",
    "text = 'Hello World'\n",
    "text.rjust(20,'=')\n",
    "text.center(20, \"#\")\n",
    "\n",
    "format(text, '>20')\n",
    "format(text, '<20')\n",
    "format(text, '^20')\n",
    "\n",
    "format(text, '=>20s')\n",
    "format(text, '*^20s')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Is#Chicago#Not#Chicago?'"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串拼接\n",
    "parts = ['Is', 'Chicago', 'Not', 'Chicago?']\n",
    "\"#\".join(parts)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Guido has 37 messages.'"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "\"text is Hello World, parts is ['Is', 'Chicago', 'Not', 'Chicago?']\""
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'text is TEXT, parts is PARTS'"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串中插入变量\n",
    "s = '{name} has {n} messages.'\n",
    "s.format(name='Guido', n=37)\n",
    "\n",
    "import string\n",
    "s = string.Template('text is $text, parts is $parts')\n",
    "s.substitute(vars())\n",
    "type(vars())\n",
    "s.substitute({\"text\": \"TEXT\", \"parts\": \"PARTS\"})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
